import { _decorator, Component, Label, Node, Sprite } from 'cc';
import ScrollLabel from 'db://cocos-ext/component/ScrollLabel';
import AssetModule from 'db://cocos-ext/base/AssetModule';
import { games, lobby, players, rooms, yy } from 'yycore';

import { EVENT_UPDATE_UI_MONEY_LOCK, IMoneyLockParam } from '../../CustomEvent';

const { ccclass, property } = _decorator;

@ccclass('CoinNode')
export class CoinNode extends Component {

    // 资源图标
    @property(Sprite)
    private icon: Sprite = null;

    // 文本
    @property(Label)
    private num: Label = null;

    // 货币类型
    @property
    private moneyType: number = 0;

    // 文本前缀
    @property
    private prefix: string = '';


    private _scrollLabal: ScrollLabel = null;
    private get isScrollValid(): boolean {
        return !!this && this.isValid && !!this._scrollLabal && this._scrollLabal.isValid;
    }


    protected onLoad(): void {
        if (!this.num) return;
        this._scrollLabal = ScrollLabel.create(this.num.node);
        this.updateScrollLabel();

        // 监听玩家货币更新事件
        players.myself.on(this, players.events.MoneyChanged, this.onPlayerMoneyChanged);
        // 监听自定义货币锁事件
        yy.event.on(this, lobby.events.CustomEvent, (e) => {
            if (e.eventID == EVENT_UPDATE_UI_MONEY_LOCK && this.isScrollValid && !!e.param) {
                const param: IMoneyLockParam = e.param as IMoneyLockParam;
                if (!param) return;
                const moneyType: number = param.moneyType;
                if (moneyType != this.moneyType) return;

                const scroll: boolean = !param.isLock;
                // 增量不需要处理0值
                if (!!param.amount)
                    this._scrollLabal.valueTemp = param.amount;
                this._scrollLabal.scroll = scroll;
            }
        });
    }

    protected start(): void {
        this.onPlayerMoneyChanged();
    }

    // 玩家货币变化事件
    private onPlayerMoneyChanged(e?: players.events.MoneyChanged) {
        if (!this.isScrollValid)
            return;

        if (!e) {
            this._scrollLabal.value = this.money;
        } else {
            this._scrollLabal.scrollTo(this.money);
        }
    }

    // 获取玩家当前游戏货币值
    private get money(): number {
        const room = rooms.service.getRoomServerInfo();
        if (rooms.service.isInRoom()
            && room
            && room.playMoneyType == this.moneyType
        ) {
            return players.myself.currentMoneyCount;
        }
        else {
            return players.myself.getBodyMoney(this.moneyType);
        }
    }

    /**
     * 设置节点对应货币类型
     * （该接口用于动态创建节点）
     * @param id 货币ID
     * @param prefix 货币前缀
     */
    public setMoneyType(id: number, prefix: string = '') {
        if (this.moneyType == id) return;
        this.moneyType = id;
        this.prefix = prefix;
        this.updateScrollLabel();
        // 更新货币ID
        if (this.icon)
            this.icon.spriteFrame = AssetModule.getSpriteFrame(this.moneyType);
    }

    // 更新ScrollLabel参数
    private updateScrollLabel() {
        if (!this.isScrollValid)
            return;
        this._scrollLabal.prefix = this.prefix;

        if (this.moneyType > 0 && this.moneyType < 1000) {
            this._scrollLabal.formatFunc = (coinCount: number) => {
                return games.gf.formatCoin({ moneyType: this.moneyType, coinCount, scale: false });
            }
        } else {
            this._scrollLabal.formatFunc = null;
        }
    }

}

